package com.starblink.store.recommend.part

import android.app.Activity
import androidx.lifecycle.LifecycleOwner
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.starblink.android.basic.adapter.MutableAdapter
import com.starblink.android.basic.ext.inflater
import com.starblink.android.basic.extension.lazyOnNone
import com.starblink.android.basic.sensorsdata.spm.cfg.SpmPageDef
import com.starblink.android.basic.util.bus.FlowBus
import com.starblink.android.basic.util.bus.FlowConst
import com.starblink.android.basic.util.bus.bean.FollowStoreEvent
import com.starblink.basic.apollo.ext.ApolloValSetExt
import com.starblink.rocketreserver.fragment.MerchantWebVOF
import com.starblink.store.databinding.ItemRecommendStoreBinding
import com.starblink.store.recommend.adapter.RecommendStoreHolder
import com.starblink.store.recommend.ui.RecommendStoreVM

/***
 * 处理頂部標題
 */
object RecommendStorePart {

    fun handleVM(
        context: Activity,
        owner: LifecycleOwner,
        rv: RecyclerView,
        refresher: SmartRefreshLayout?,
        viewModel: RecommendStoreVM,
        isNestedScrollingEnabled: Boolean = true,
        pageRef: Int,
        pageTrackTag: String,
    ) {

        //同步订阅数据
        FlowBus.with<FollowStoreEvent>(FlowConst.FOLLOW_STORE_EVENT).register(owner) {
            for ((index, merchat) in viewModel.recommendList.value!!.withIndex()) {
                if (merchat != null && merchat is MerchantWebVOF && it.id == merchat.id
                    && it.followed != merchat.subOrNot
                ) {
                    ApolloValSetExt.setApolloVal<MerchantWebVOF>(merchat, "subOrNot", it.followed)
                    viewModel.recommendList.value!![index] = merchat
                    viewModel.recommendList.value = viewModel.myList.value
                }
            }
        }

        val recommendAdapter: MutableAdapter by lazyOnNone {
            MutableAdapter(viewModel.recommendList.value!!).apply {
                addVhDelegate {
                    RecommendStoreHolder(
                        ItemRecommendStoreBinding.inflate(it.inflater, it, false),
                        context, pageRef, pageTrackTag
                    )
                }
            }
        }
        val linearLayoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
        rv.layoutManager = linearLayoutManager

        rv.adapter = recommendAdapter//单位px

        refresher?.setEnableRefresh(false)
        refresher?.setOnLoadMoreListener {
            viewModel.loadRecommendStores(sceneCode = if(pageRef == SpmPageDef.StoreMainRecommendListPage) 3 else 5)
        }
        if (!isNestedScrollingEnabled) {
            rv.isNestedScrollingEnabled = false
            linearLayoutManager.isSmoothScrollbarEnabled = true
//        rv.setHasFixedSize(true)
        }

        viewModel.recommendList.observe(owner) {
            if (viewModel.recommendList.value!!.isNotEmpty()) {
//                recommendAdapter.notifyItemRangeChanged(
//                    viewModel.recPositionStart,
//                    viewModel.recItemCount
//                )
                recommendAdapter.notifyDataSetChanged()
            }

            if (refresher!!.isLoading) {
                refresher?.finishLoadMore()
            }
        }

    }
}